package Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class test1 {
    public static void main(String[] args) {

    }


    public List<Integer> findAnagrams(String s, String p) {
        List<Integer>ret=new ArrayList<>();
char[]s1=s.toCharArray();
char[]s2=p.toCharArray();
int []hash1=new int[26];
int []hash2=new int[26];


        for (int i = 0; i <s2.length ; i++) {
            hash2[s2[i]-'a']++;
        }

        int m=s2.length;
        for (int right = 0,left=0,count=0; right <s1.length ; right++) {
            int in=s1[right]-'a';
            if (hash1[in]++<hash2[in]){
                count++;
            }
            int len=right-left+1;
            if(len>m){
                int out=s1[left++]-'a';
              hash1[out]--;
                if (hash1[out]<hash2[out]){
                    count--;
                }
            }
            if (count==m){
                ret.add(left);
            }
        }
        return ret;
    }


    public static void main2(String[] args) {
int []arr={3,3,3,1,2,1,1,2,3,3,4};
        System.out.println(totalFruit(arr));

    }

    public static int totalFruit(int[] fruits) {
int ret=0;
        Map<Integer,Integer>hash=new HashMap<>();
        for (int right = 0,left=0; right <fruits.length ; right++) {
        int in=fruits[right];
        hash.put(fruits[right],hash.getOrDefault(in,0)+1);
        while (hash.size()>2){
            int out=fruits[left++];
            hash.put(out,hash.get(out)-1);
            if(hash.get(out)==0){
                hash.remove(out);
            }
        }
        ret=Math.max(ret,right-left+1);
        }
        return ret;
    }

    public static void main1(String[] args) {
int []nums={3,2,20,1,1,3};
        System.out.println(minOperations(nums, 10));
    }

    public static int minOperations(int[] nums, int x) {
       int sum=0;
        for (int t:nums) {
            sum+=t;
        }
        int target=sum-x;
if (target<0){
    return -1;
}
int ret=-1;
int tmp=0;
        for (int left = 0,right=0; right <nums.length ; right++) {
    tmp+=nums[right];
    while (tmp>target){
        tmp-=nums[left++];
    }
    if(tmp==target){
        ret=Math.max(ret,right-left+1);
    }
        }
return ret==-1?ret: nums.length-ret;


    }


}
